﻿/*
 * Based on code obtained from http://blogs.microsoft.co.il/blogs/arik/archive/2010/05/28/wpf-single-instance-application.aspx
 * Copyright is unknown.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Shell;
using System.Windows;

namespace File_CRC32_Verifier_WPF
{
    public partial class Program : Application, ISingleInstanceApp
    {
        private const string Unique = "CRC32Verifier";
        private static File_CRC32_Verifier_WPF.MainWindow mainWindow = new File_CRC32_Verifier_WPF.MainWindow();

        [STAThread]
        public static void Main()
        {
            if (SingleInstance<Program>.InitializeAsFirstInstance(Unique))
            {
                var application = new Program();
                mainWindow.Start();
                application.Run(mainWindow);

                // Allow single instance code to perform cleanup operations
                SingleInstance<Program>.Cleanup();
            }
        }

        #region ISingleInstanceApp Members

        /// <summary>
        /// Secondary instances of the program will signal their command line arguments to this method.
        /// The files sent in the arguments will be hashed.
        /// </summary>
        /// <param name="args">The command line arguments as an IList of type string.</param>
        /// <returns>True.</returns>
        public bool SignalExternalCommandLineArgs(IList<string> args)
        {
            //Remove the execultable itself from the list.
            List<string> tmp = new List<string>(args);
            if (tmp.Count > 1)
            {
                tmp.RemoveAt(0);
                mainWindow.hashList(tmp);
            }

            return true;
        }

        #endregion
    }
}